package Calculator.Tests;

import static org.junit.Assert.*;
import junit.framework.TestSuite;

import org.junit.Test;

import Calculator.Calculator;
import Calculator.Exceptions.DivisionByZeroException;
import Calculator.Exceptions.UnclosedParenthesisException;
import Calculator.Exceptions.WrongExpressionException;
import Calculator.Exceptions.WrongSymbolException;
import Calculator.Parser.Utilities;

public class CalculatorTest extends TestSuite{

	@Test
	public void commonCalculus() {
		Calculator c = new Calculator();
		Double result = c.evaluate("(3 * 9) * (7 / 2) / (37 + 15 - 9)");
		result = Utilities.roundToTwoDecimals(result);
		System.out.println(result);
		assertEquals(2.19, result, 0.01);
	}
	
	@Test
	public void commonCalculusTwo() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2 * 3 - (5 - 2 * 4) * 3");
		result = Utilities.roundToTwoDecimals(result);
		System.out.println(result);
		assertEquals(19, result, 0);
	}
	
	@Test
	public void commonCalculusThree() {
		Calculator c = new Calculator();
		Double result = c.evaluate("((3 * 5 / 2) * (38 / 4) / (30 * 3)) * 30 / 18");
		result = Utilities.roundToTwoDecimals(result);
		System.out.println(result);
		assertEquals(1.31, result, 0.01);
	}
	
	@Test
	public void commonCalculusFour() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2 - (5 * (4 - 3 / 2)) + 8 / (4 - 2)");
		result = Utilities.roundToTwoDecimals(result);
		System.out.println(result);
		assertEquals(-2.5, result, 0);
	}
	
	@Test
	public void commonCalculusFive() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4+2-(5*(4-3/2))+8/(4-2)");
		result = Utilities.roundToTwoDecimals(result);
		System.out.println(result);
		assertEquals(-2.5, result, 0);
	}
	
	@Test(expected=DivisionByZeroException.class)
	public void testDivisionByZeroException() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2 - (5 * (4 - 3 / 2)) + 8 / (0)");
		assertNull(result);
	}

	@Test(expected=UnclosedParenthesisException.class)
	public void testUnclosedParenthesisException() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2 - (5 * (4 - 3 / 2) + 8 / 1");
		assertNull(result);
	}
	
	@Test(expected=WrongExpressionException.class)
	public void testWrongExpressionException() {
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2 - (5 (4 - 3 / 2)) + 8 / 1");
		assertNull(result);
	}
	
	@Test(expected=WrongSymbolException.class)
	public void testWrongSymbolException(){
		Calculator c = new Calculator();
		Double result = c.evaluate("4 + 2y - (5 * (4 - 3 / 2)) + 8 / 1");
		assertNull(result);
	}
}
